<?php
/**
 * NAME: timer
 * WHEN: Every5000 OnMsg
 * PROVIDES: !at !timer
 * AUTOLOAD: true
 * DESCRIPTION: Oferuje funkcje timera i powiadamiacza:)
 * VERSION: 0.1
 * UPDATE: http://update.tempaq2.blacksoft.eu/check.php
 */
 
 exit();
 
//*function_start
	if (isset($data)) {
		//'timer' => 'time int, what text, count int',
		//$table = new table('timer');
		sqlite::create('timer',array(
			'czas'=>'INT',
			'what'=>'VARCHAR',
			'count'=>'INT',
		));
		if ($d[0]=='!at') {
			$time = $tekst = '';
			$error = false;
			if (ereg('[12]{0,2}[0-9]{1}:[0-9][0-9]', $d[1])) {
				$time = explode(':', $d[1]);
				$time = mktime($time[0], $time[1]);
				$tekst = substr($data->message, strpos($data->message, $d[2]));
			//	echo $time."\n".mktime()."\n"
			} elseif (ereg('[12]{0,2}[0-9]{1}:[0-9][0-9]', $d[2])) {
				$tab = explode('.', $d[1]);
				$time = explode(':', $d[2]);
				switch (count($tab)) {
					case 1:
						$time = mktime($time[0], $time[1], date('s'), date('m'), $tab[0], date('Y'));	
						break;
					case 2:
						$time = mktime($time[0], $time[1], date('s'), $tab[1], $tab[0], date('Y'));	
						break;
					case 3:
						$time = mktime($time[0], $time[1], date('s'), $tab[1], $tab[0], $tab[2]);	
						break;
					default:
						$error = true;
				}
				$tekst = substr($data->message, strpos($data->message, $d[3]));
			} else {
				//bledna skladnia
			}
			if (!empty($tekst)&&!$error) {
				if (sqlite::insert('timer',array('czas'=>$time, 'what'=>$tekst, 'count'=>0))) {
					$bot->send_text('timer_added');
				}
			} else {
				//bledna skladnia			
			}
		} else if ($d[0]=='!timer') {
			if (isset($d[1])) {
				$time = explode(':', $d[1]);
				$time = array_reverse($time);
				$time = mktime(date('H')+(isset($time[2])?$time[2]:0),date('i')+(isset($time[1])?$time[1]:0), date('s')+$time[0], date('n'), date('j')+(isset($time[3])?$time[3]:0));
				$tekst = substr($message, strpos($message, $d[2]));
			//	echo $time."\n".time()."\n";
				if (isset($d[2]))
					if (sqlite::insert('timer',array('czas'=>$time, 'what'=>$tekst, 'count'=>0))) {
						$bot->send_text('timer_added');
					}
			}
		}
		//unset($table);
	} else { //zdarzenie czasowe
		
		$now = time(); //tak żeby nie było że czas przeleci, coś usuniemy a zonk będzie z wydarzeniem
		$tab = sqlite::fetchSQL('SELECT * FROM timer WHERE czas<="'.$now.'"');
		
		foreach ($tab as $v) $bot->sendmessage(date("H:i", $v['czas'])." - ".$v['what']);
		sqlite::query('DELETE FROM timer WHERE czas<="'.$now.'"');
			
		
	}
//*function_end	
?>